<?php
namespace Admin\Controller;

class WelfareController extends AdminController{

	public function index(){
        $list   = $this->lists('welfare');
        foreach ($list as $key => &$value) {
        	$value['sum'] = M('welfare_list')->where(array('wid'=>$value['id']))->sum('price');
        }
        $this->assign('_list', $list);
        $this->meta_title = '公益捐赠';
		$this->display();
	}

	public function welfare_info(){
		$id = I('id',0);
		$welfare_list = D('WelfareList')->relation(true)->where(array('wid'=>$id))->select();
		$this->assign('welfare_list',$welfare_list);
		$this->display();
	}

	public function welfare_add(){
		if (IS_POST) {
			$data = M('welfare')->create();
			$data['price'] = floatval($data['price']);
			if (empty($data['title'])) {
				$this->error("请正确填写公益标题");
				exit();
			}
			if ($data['price'] <= 0) {
				$this->error("目标金额不正确");
				exit();
			}
			$data['time'] = NOW_TIME;
			if(M('welfare')->add($data)){
				$this->success("添加成功",U('index'));
			}else{
				$this->error("添加失败");
			}
			exit();
		}
		$this->display('welfare_edit');
	}
	public function welfare_update(){
		if (IS_POST) {
			$data = M('welfare')->create();
			$data['price'] = floatval($data['price']);
			if (empty($data['title'])) {
				$this->error("请正确填写公益标题");
				exit();
			}
			if ($data['price'] <= 0) {
				$this->error("目标金额不正确");
				exit();
			}
			if(M('welfare')->save($data)){
				$this->success("操作成功",U('index'));
			}else{
				$this->error("操作失败");
			}
			exit();
		}
		$id = I('id',0);
		$welfare_info = M('welfare')->find($id);

		include 'Ueditor/Ueditor.class.php';
		$ue = new \Ueditor();
		$ue->init ();
		$editor = $ue->editor ( 'description',$welfare_info['description']);
		$this->assign('field',$welfare_info);
		$this->assign ( 'description', $editor );
		$this->display('welfare_edit');
	}
}